/// <reference types="vitest/config" />

import { resolve } from "path"
import vue from "@vitejs/plugin-vue"
import vueJsx from "@vitejs/plugin-vue-jsx"
import AutoImport from "unplugin-auto-import/vite"
import { createSvgIconsPlugin } from "vite-plugin-svg-icons"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import { defineConfig, loadEnv } from "vite"
import cesium from "vite-plugin-cesium"
import svgLoader from "vite-svg-loader"

// Configuring Vite: https://cn.vite.dev/config
export default defineConfig(async ({ mode }) => {
  const { VITE_PUBLIC_PATH } = loadEnv(mode, process.cwd(), "")

  // 动态导入UnoCSS
  const UnoCSS = await import("unocss/vite").then(m => m.default);

  return {
    // 开发或打包构建时用到的公共基础路径
    base: VITE_PUBLIC_PATH,
    resolve: {
      alias: {
        // @ 符号指向 src 目录
        "@": resolve(__dirname, "src"),
        // @@ 符号指向 src/common 通用目录
        "@@": resolve(__dirname, "src/common")
      }
    },
    // 开发环境服务器配置
    server: {
      // 是否监听所有地址
      host: true,
      // 端口号
      port: 3230,
      // 端口被占用时，是否直接退出
      strictPort: false,
      // 是否自动打开浏览器
      open: true,
      // 反向代理
      proxy: {
        // 模型
        "/model-api": {
          // target: "http://10.5.56.58:9288",
          target: "http://10.11.7.55:9288",
          // target: "http://123.57.218.86:9288",
          changeOrigin: true,
          rewrite: path => path.replace(/^\/model-api/, "")
        },
        // 训练
        "/train-api": {
          // target: "http://10.5.56.58:8890",
          target: "http://10.11.7.55:8890",
          // target: "http://123.57.218.86:8890",
          changeOrigin: true,
          rewrite: path => path.replace(/^\/train-api/, "")
        },
        "/seen-api": {
          // target: "http://10.5.56.58:7970",
          target: "http://10.11.7.55:7970",
          // target: "http://123.57.218.86:7970",
          changeOrigin: true,
          rewrite: path => path.replace(/^\/seen-api/, "")
        }
      },
      // 是否允许跨域
      cors: true,
      // 预热常用文件，提高初始页面加载速度
      warmup: {
        clientFiles: [
          "./src/layouts/**/*.*",
          "./src/pinia/**/*.*",
          "./src/router/**/*.*"
        ]
      }
    },
    // 构建配置
    build: {
      minify: false,
      sourcemap: true,
      rollupOptions: {
        output: {
          manualChunks: undefined
        }
      },
      cssCodeSplit: true,
      cssMinify: "esbuild",
      chunkSizeWarningLimit: 2000,
      reportCompressedSize: true,
      modulePreload: {
        polyfill: true,
        resolveDependencies: (filename, deps, { hostId, hostType }) => {
          return deps
        }
      },
      target: "es2015",
      typescript: {
        ignoreBuildErrors: true
      }
    },
    // 混淆器
    esbuild:
      mode === "development"
        ? undefined
        : {
            // 打包构建时移除 console.log
            pure: ["console.log"],
            // 打包构建时移除 debugger
            drop: ["debugger"],
            // 打包构建时移除所有注释
            legalComments: "none"
          },
    // 插件配置
    plugins: [
      vue(),
      cesium(),
      // 支持 JSX、TSX 语法
      vueJsx(),
      // 支持将 SVG 文件导入为 Vue 组件
      svgLoader({
        defaultImport: "url",
        svgoConfig: {
          plugins: [
            {
              name: "preset-default",
              params: {
                overrides: {
                  // @see https://github.com/svg/svgo/issues/1128
                  removeViewBox: false
                }
              }
            }
          ]
        }
      }),
      createSvgIconsPlugin({
        // 指定 SVG图标 保存的文件夹路径
        iconDirs: [resolve(__dirname, "src/common/assets/icons")],
        // 指定 使用svg图标的格式
        symbolId: 'icon-[dir]-[name]'
      }),
      // 原子化 CSS
      UnoCSS(),
      // 自动按需导入 API
      AutoImport({
        imports: ["vue", "vue-router", "pinia"],
        dts: "types/auto/auto-imports.d.ts",
        resolvers: [ElementPlusResolver()]
      })
    ],
    // Configuring Vitest: https://cn.vitest.dev/config
    test: {
      include: ["tests/**/*.test.{ts,js}"],
      environment: "happy-dom",
      server: {
        deps: {
          inline: ["element-plus"]
        }
      }
    }
  }
})
